package com.jnu.myaccount.function.sort;

import android.os.Bundle;
import android.view.LayoutInflater;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager.widget.ViewPager;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.jnu.myaccount.R;
import com.jnu.myaccount.adapter.RecordPagerAdapter;
import com.jnu.myaccount.databinding.FragmentSortBinding;
import com.jnu.myaccount.record.IncomeFragment;
import com.jnu.myaccount.record.PaidFragment;

import java.util.ArrayList;
import java.util.List;

public class SortFragment extends Fragment {
    private SortViewModel sortViewModel;
    private FragmentSortBinding binding;
    private TabLayout tabLayout;
    private ViewPager viewPager;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        sortViewModel = new ViewModelProvider(this).get(SortViewModel.class);

        binding = FragmentSortBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

//1.查找控件
        tabLayout = root.findViewById(R.id.sort_tab);
        viewPager = root.findViewById(R.id.sort_vp);

        //2.设置ViewPager加载页面
        initPager();
        return root;
    }

    private void initPager() {
        //        初始化ViewPager页面的集合
        List<Fragment> fragmentList = new ArrayList<>();
//        创建收入和支出页面，放置在Fragment当中
        SortPaidFragment outFrag = new SortPaidFragment(); //支出
        SortIncomeFragment inFrag = new SortIncomeFragment(); //收入
        fragmentList.add(outFrag);
        fragmentList.add(inFrag);

//        创建适配器
        RecordPagerAdapter pagerAdapter = new RecordPagerAdapter(getChildFragmentManager(), fragmentList);//fragment使用viewpage切记需要的是getChildFragmentManager()
//        设置适配器
        pagerAdapter.setTitles();
        viewPager.setAdapter(pagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}